package com.linghang.association.utils;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.linghang.association.domain.User;
import com.linghang.association.req.UserAdminReq;
import com.linghang.association.req.UserLoginReq;
import com.linghang.association.resp.UserAdminResp;
import com.linghang.association.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Base64;

/**
 * @author LinyaT052
 * @date 2022年07月19日 18:53
 * token解析工具类
 */
public class PaeseTokenUtil {
    private static final Logger LOG = LoggerFactory.getLogger(PaeseTokenUtil.class);
    public static UserAdminResp getUser(String token){
        DecodedJWT decodedJWT = JwtUtil.getPayload(token);
        //获取token中的负载加密字段
        String token_payload = decodedJWT.getPayload();
        Base64.Decoder decoder = Base64.getUrlDecoder();
        //获取负载解密字段
        String payload = new String(decoder.decode(token_payload));
        //解析json数据得到UserLoginReq实体
        JSONObject jsonObject = JSONObject.parseObject(payload);
        UserAdminResp resp = new UserAdminResp(Integer.parseInt(jsonObject.getString("id")),jsonObject.getString("username"),
                Integer.parseInt(jsonObject.getString("departmentId")));
        LOG.info("本次解析得到的响应实体为"+resp);
        return resp;
    }
}
